<template>
	<div class="app-container">
		<div class="head-container">
			<div v-if="crud.props.searchToggle">
				<el-input v-model="query.name" clearable size="small" placeholder="全表模糊搜索" style="width: 200px;"
					class="filter-item" @keyup.enter.native="crud.toQuery" />
				<rrOperation />
			</div>
			<crudOperation :permission="permission" />
		</div>

		<!-- 表单 -->
		<el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU"
			:visible.sync="crud.status.cu > 0" :title="crud.status.title" width="680px">
			<el-form ref="form" :model="form" label-width="120px">
				<el-form-item label="名称">
					<el-input v-model="form.name" style="width: 370px;" />
				</el-form-item>
				<el-form-item label="图标">
					<!-- <el-input v-model="form.icon" style="width: 370px;" /> -->
					<el-upload ref="upload" accept=".jpg,.jpeg,.png,.JPG,.JPEG" :http-request="uploadImg" action="#"
						:auto-upload="false" :show-file-list="true" list-type="picture-card" :on-remove="handleRemove">
						<img v-if="form.icon" :src="form.icon" class="avatar" />
						<i class="el-icon-plus" />
					</el-upload>
					<el-button style="margin-left: 10px; margin-top: 10px;" size="small" type="success"
						@click="submitUpload">上传
					</el-button>
				</el-form-item>
				<el-form-item label="标签">
					<el-input v-model="form.label" style="width: 370px;" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="text" @click="crud.cancelCU">取消</el-button>
				<el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
			</div>
		</el-dialog>

		<!--表格渲染-->
		<el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;"
			@selection-change="crud.selectionChangeHandler">
			<el-table-column type="selection" width="55" />
			<el-table-column prop="name" label="名称" />
			<el-table-column prop="icon" label="图标">
				<template slot-scope="scope">
					<el-image style="width: 100px; height: 100px" :src="scope.row.icon" fit="contain" />
				</template>
			</el-table-column>
			<el-table-column prop="label" label="标签" />
			<el-table-column v-if="checkPer(['admin','ftServiceType:edit','ftServiceType:del'])" label="操作"
				width="150px" align="center">
				<template slot-scope="scope">
					<udOperation :data="scope.row" :permission="permission" />
				</template>
			</el-table-column>
		</el-table>
		<!--分页组件-->
		<pagination />
	</div>
</template>

<script>
	import crudServiceType from '@/api/finace_taxation/service_type.js'
	import CRUD, {
		presenter,
		header,
		form,
		crud
	} from '@crud/crud'
	import rrOperation from '@crud/RR.operation'
	import crudOperation from '@crud/CRUD.operation'
	import pagination from '@crud/Pagination'
	import udOperation from '@crud/UD.operation'
	import axios from 'axios'

	// crud交由presenter持有
	const defaultForm = {
		name: null,
		icon: null,
		label: null
	}

	export default {
		name: 'FtServiceType',
		components: {
			pagination,
			crudOperation,
			rrOperation,
			udOperation,
		},
		cruds() {
			return CRUD({
				url: 'api/ft/ftServiceType',
				title: '服务分类',
				crudMethod: {
					...crudServiceType
				}
			})
		},
		mixins: [presenter(), header(), form(defaultForm), crud()],
		data() {
			return {
				delLoading: false,
				permission: {
					add: ['admin', 'ftServiceType:add'],
					edit: ['admin', 'ftServiceType:edit'],
					del: ['admin', 'ftServiceType:del']
				},
				cityList: []
			}
		},
		created() {
			this.crud.optShow = {
				add: true,
				reset: true
			}
		},
		methods: {
			uploadImg(file) {
				this.formDate.append('file', file.file)
			},
			handleRemove(file, fileList) {},
			submitUpload() {
				this.formDate = new FormData()
				this.$refs.upload.submit()
				this.formDate.append('WS_CODE', '12133')
				const config = {
					headers: {
						'Content-Type': 'multipart/form-data'
					}
				}
				axios.post('http://localhost:8000/api/ft/uploadUtil/uploadImage', this.formDate, config).then(res => {
					this.crud.form.icon = res.data.result
					this.$refs.upload.clearFiles()
					this.crud.notify('图片上传成功', 'success')
				}).catch(res => {
					this.crud.notify('图片上传失败', 'error')
				})
			},
		}
	}
</script>
<style>
	.avatar {
		width: 146px;
		height: 146px;
		display: block;
	}
</style>
